package com.weblog.weblog.common.domain.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weblog.weblog.common.domain.dos.WikiDO;

import java.time.LocalDate;
import java.util.List;
import java.util.Objects;

public interface WikiMapper extends BaseMapper<WikiDO> {

    default Page<WikiDO> selectPageList(Long current, Long size, String title, LocalDate startDate,LocalDate endDate,Boolean isPublish){
        Page<WikiDO> page = new Page<>(current,size);

        LambdaQueryWrapper<WikiDO>  wrapper = Wrappers.<WikiDO>lambdaQuery()
                .like(StringUtils.isNotBlank(title), WikiDO::getTitle,title)    // like 模块查询
                .ge(Objects.nonNull(startDate),WikiDO::getCreateTime,startDate)  //大于等于StartDate
                .le(Objects.nonNull(endDate),WikiDO::getCreateTime,endDate)       //小于等于 endDate
                .eq(Objects.nonNull(isPublish),WikiDO::getIsPublish,isPublish)      //发布状态
                .orderByDesc(WikiDO::getWeight)   //按照权重排序
                .orderByDesc(WikiDO::getCreateTime);   //按照创建时间排序


        return selectPage(page,wrapper);
    }


    default WikiDO selectMaxWeight(){
        return  selectOne(Wrappers.<WikiDO>lambdaQuery()
                .orderByDesc(WikiDO::getWeight)
                .last("LIMIT 1"));
    }


    /**
     * 查询已发布的知识库
     * @author gc
     * @date 2024/2/5 20:17
     * @return java.util.List<com.weblog.weblog.common.domain.dos.WikiDO>
     */

    default List<WikiDO> selectPublished(){
        return selectList(Wrappers.<WikiDO>lambdaQuery()
                .eq(WikiDO::getIsPublish,1)   //查询已发布的
                .orderByDesc(WikiDO::getWeight)   //按权重降序
                .orderByDesc(WikiDO::getCreateTime)  //按发布时间降序
        );
    }
}
